package kr.hkit.BusinessManagement.BasicDataMgnUI;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import kr.hkit.BusinessManagement.DBCon.BusinessEmpDBCon;
import kr.hkit.BusinessManagement.DBCon.ProductDBCon;
import kr.hkit.BusinessManagement.DTO.BusinessEmpDTO;
import kr.hkit.BusinessManagement.DTO.ProductDTO;

public class PanelProductMgn extends JPanel implements ActionListener {
	private JTextField tfCode;
	private JTextField tfName;
	private JLabel lblBECode;
	private JLabel lblBEName;
	private JLabel lblBELevel;
	private JButton btnSave;
	private JButton btnDelete;
	private JButton btnSearch;

	ProductDBCon DBCon = new ProductDBCon();
	private JTextField tfSellPrice;
	private JLabel lblNewLabel;
	private JTextField tfOfferPrice;
	/**
	 * Create the panel.
	 */
	public PanelProductMgn() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.SOUTH);
		
		btnSave = new JButton("저장");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ProductDTO product = getText();
				DBCon.addProduct(product);
			}
		});
		panel.add(btnSave);
		
		btnDelete = new JButton("삭제");
		btnDelete.addActionListener(this);
		panel.add(btnDelete);
		
		btnSearch = new JButton("검색");
		btnSearch.addActionListener(this);
		panel.add(btnSearch);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		lblBECode = new JLabel("제품코드");
		lblBECode.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblBECode);
		
		tfCode = new JTextField();
		panel_1.add(tfCode);
		tfCode.setColumns(10);
		
		lblBEName = new JLabel("제품명");
		lblBEName.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblBEName);
		
		tfName = new JTextField();
		panel_1.add(tfName);
		tfName.setColumns(10);
		
		lblBELevel = new JLabel("판매정가");
		lblBELevel.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblBELevel);
		
		tfSellPrice = new JTextField();
		panel_1.add(tfSellPrice);
		tfSellPrice.setColumns(10);
		
		lblNewLabel = new JLabel("판매원가");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblNewLabel);
		
		tfOfferPrice = new JTextField();
		panel_1.add(tfOfferPrice);
		tfOfferPrice.setColumns(10);

	}

	public ProductDTO getText(){
		
		String code = tfCode.getText().toString().trim();
		String name = tfName.getText().toString().trim();
		int sellPrice = Integer.parseInt(tfSellPrice.getText());
		int offerPrice = Integer.parseInt(tfOfferPrice.getText());
		
		return new ProductDTO(code, name, sellPrice, offerPrice);
		
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnDelete) {
			actionPerformedBtnDelete(arg0);
		}
		if (arg0.getSource() == btnSearch) {
			actionPerformedBtnSearch(arg0);
		}
	}
	protected void actionPerformedBtnSearch(ActionEvent arg0) {
		String code = tfCode.getText().trim();
		ProductDTO product = new ProductDTO();
		product = DBCon.searchProductByCode(code);
		
		tfCode.setText(product.getCode());
		tfName.setText(product.getName());
		tfSellPrice.setText(Integer.toString(product.getSellPrice()));
		tfOfferPrice.setText(Integer.toString(product.getOfferPrice()));
		
	}
	protected void actionPerformedBtnDelete(ActionEvent arg0) {
		ProductDTO product = new ProductDTO();
		product = getText();
		DBCon.deleteProduct(product);
	}
}
